import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl, appletApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';

@Injectable()

export class ProductlistService {

  constructor(private httpService: HttpService) {

  }

  type1 = '';
  type2 = '';


  getdata() {

  }

  getGoodsList(page: number,
               pageSize: number,
               shop_id: number,
               category_id?: number,
               status?: string,
               order?: string,
               keyword?: string,
  ) {
    const _category_id = category_id || '';
    console.log('_status', status);
    let _status;
    if (status === '下架') {
      _status = 0;
    } else if (status === '上架') {
      _status = 1;
    }
    const _keyword = keyword || '';
    const _order = (order === 'none' ? '' : order) || '';
    const url = `${appletApiUrl}/backend/goods/index`;
    // +`?applet_goods_category_id=${_applet_goods_category_id}` +
    // `&order=${_order}&status=${_status}&keyword=${_keyword}&page=${page}&pageSize=${pageSize}`
    let data = {
      'shop_id': shop_id,
      'page': page,
      'category_id': _category_id,
      'keyword': _keyword,
      'for_sale': _status,
    };
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

  // 获取商品绑定了哪些分类
  getbindListAll(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindListAll?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取分组列表
  getGroupList(id) {
    const url = `${appletApiUrl}/backend/category/index?pageSize=999`;
    return this.httpService.httpPostAuth(url, {shop_id: id}).map(rs => rs);
  }

  // 新建分组
  postAddGroup(item: any) {
    const url = `${appletApiUrl}/backend/category/create`;
    return this.httpService.httpPostAuth(url, item).map(rs => rs);
  }

  // 删除分组
  delGroup(id: number, shopid) {
    const url = `${appletApiUrl}/backend/category/delete`;
    return this.httpService.httpPostAuth(url, {id: id, shop_id: shopid}).map(rs => rs);
  }

  // 修改分组
  editGroup(item: any) {
    const url = `${appletApiUrl}/backend/category/update`;
    return this.httpService.httpPostAuth(url, item).map(rs => rs);
  }

  getGoodsInfoById(shop_id, id) { // 获取商品详情
    const url = `${appletApiUrl}/backend/goods/detail`;
    return this.httpService.httpPostAuth(url, {'shop_id': shop_id, "id": id}).map(rs => rs);
  }


  delProduct(data) {
    const url = `${appletApiUrl}/backend/goods/delete`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }


  // 改变商品上下架状态
  changeGoodsStatus(data) {
    const url = `${appletApiUrl}/backend/goods/change_sale_state`;
    return this.httpService.httpPostAuth(url, data).map(rs => rs);
  }

}
